package gl.j2ee.jfinal;

import gl.web.util.DoubleArrayTrieWordFilter;
import gl.web.util.FileUtils;
import gl.web.util.OpenKeyGen;
import gl.web.util.TextUtils;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json2pojo.autobuild.http.download.ControllerDownLoad;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.render.Render;
import com.jfinal.upload.UploadFile;

public class MainController extends Controller {

	@ClearInterceptor
	public void index() {
		renderJsp("index.jsp");
	}

	@ActionKey("/json2bean")
	public void json2bean() {
		renderJsp("json2bean/index.html");
	}
	@ActionKey("/json2bean/dojson2bean")
	public void dojson2bean() {
		new ControllerDownLoad().doHttp(this,MyBaseConfig.TEMPDIR);
	}
	public void json() {
		final String json = "{\"json\":{\"a\":\"b\"}}";
		render(new Render() {
			private static final long serialVersionUID = 1L;

			@Override
			public void render() {
				try {
					response.getOutputStream().write(json.getBytes());
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});
	}

	@ActionKey("/login")
	public void login() {
		renderText("<a href=\"vszone://opengame?id=10\">test</a>");
		// renderText("<a href=\"/code.png\">test</a>");
	}
	@ActionKey("/openkeygen")
	public void openkeygen() {
		// renderText("<a href=\"vszone://opengame?id=10\">test</a>");
		renderJsp("openkeygen.html");
		System.out.println("renderJsp");
	}

	@ActionKey("/keyword")
	public void keyword() {
		String content = getPara("content");
		if (TextUtils.isEmpty(content)) {
			renderText("content is empty");
			return;
		}
		List<String> list= DoubleArrayTrieWordFilter.getInstance().filter(content.toLowerCase());
		JSONArray ja = new JSONArray();
		for (String string : list) {
			ja.put(string);
		}
		renderJson(ja.toString());
	}
	@ActionKey("/listscript")
	public void listscript() {
		String[] list= new File(new File(getClass().getResource("/").getPath()).getParentFile().getParent()+File.separator).list(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".test");
			}
		});
		JSONArray ja = new JSONArray();
		for (String string : list) {
			System.out.println("filename:" +string);
			ja.put(string);
		}
		renderJson(ja.toString());
	}

	@ActionKey("/doopenkeygen")
	public void doopenkeygen() {
		String apppackage = getPara("package");
		String sign = getPara("sign");
		String channel = getPara("channel");
		if (TextUtils.isEmpty(apppackage)) {
			renderText("app package is empty");
			return;
		}
		if (TextUtils.isEmpty(sign)) {
			renderText("key fingerprint  is empty");
			return;
		}
		if (TextUtils.isEmpty(channel)) {
			renderText("channel  is empty");
			return;
		}
		if (sign.contains(":")) {
			sign = sign.replaceAll(":", "");
		}
		try {
			String genkey = OpenKeyGen.gen(apppackage, sign.toUpperCase());
			renderText(genkey);
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("channel", channel);
			map.put("package", apppackage);
			map.put("sign", sign);
			map.put("genkey", genkey);
			map.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(new Date()));
			File file = new File(MyBaseConfig.FILEDB);
			String jsonString = JSON.toJSONString(map);
			FileUtils.appTextToFile(file, jsonString + ",");
		} catch (Exception e) {
			e.printStackTrace();
			renderText("" + e.getMessage());
		}
		return;
	}

	@ActionKey("/qrimage")
	public void qrimage() {
		// renderText("<a href=\"vszone://opengame?id=10\">test</a>");
		renderJsp("qrimage.html");
		System.out.println("renderJsp");
	}

	@ActionKey("/code")
	public void code() {
		// renderText("<a href=\"vszone://opengame?id=10\">test</a>");
		renderJsp("app.html");
		System.out.println("renderJsp");
	}

	@ActionKey("/keygenhistory")
	public void keygenhistory() {

		String data = "[";
		try {
			data += FileUtils.readUTF8FromFile(new File(MyBaseConfig.FILEDB));
			data = data.substring(0,data.lastIndexOf(","));
			data += "]";
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			renderText(data);
		} catch (Exception e) {
			renderText("" + e.getMessage());
			e.printStackTrace();
		}
	}
	@ActionKey("/fileupload")
	public void fileupload() {
		// renderText("<a href=\"vszone://opengame?id=10\">test</a>");
		renderJsp("fileupload.html");
		System.out.println("renderJsp");
	}
	@ActionKey("/dofileupload")
	public void dofileupload() {
		try {
			UploadFile up=getFile();
			String fileName = getPara("filename");
			File sign = up.getFile();
			System.out.println(sign.toString());
			File DstFile = sign.getParentFile().getParentFile();
			System.out.println(DstFile);
			org.apache.commons.io.FileUtils.copyFileToDirectory(sign, DstFile);
			renderText("save success");
		} catch (Exception e) {
			renderText("" + e.getMessage());
			e.printStackTrace();
		}
	}

}
